<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/9/27
 * Time: 14:31
 */

namespace app\wap\controller;


use Symfony\Component\Yaml\Tests\DumperTest;
use think\Controller;
use app\api\controller\KbxToken as KbxToken;

class Kbxcoupon extends Controller
{
    private  $api_url='https://d.kwibon.cn/api';//测试地址

    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
    }

    //兑换优惠券
    public function coupon(){
        $Notify=  new Notify();
        $url=$this->api_url."/client/coupon/toCoupon";
        $post=input('post.');

        if (empty($post))return json_encode(['msg'=>'参数错误不能为空','code'=>'101']);
        if (empty($post['member_id'])) return json_encode(['msg'=>'用户id出错','code'=>'101']);
        if (empty($post['code'])) return json_encode(['msg'=>'兑换码不能为空','code'=>'101']);
        if (empty($post['pwd'])) return json_encode(['msg'=>'兑换码密码不能为空','code'=>'101']);
        $token=$Notify->kbxToken();

        $user = db("member")->where('member_id',$post['member_id'])->field("member_username")->find();
        $kbxToken = new KbxToken();
        //获取kbx的user_id
        $kbx = $kbxToken->kbxUser($user['member_username']);

        if (empty($kbx)) return json_encode(['msg'=>'用户不存在','code'=>'101']);
        $data=[
            'userId'=>$kbx['id'],
            'code'=>$post['code'],
            'pwd'=>$post['pwd'],
        ];

        $datas=json_encode($data);
        $po= $Notify->post_data($url,$datas,$token);
        $po= json_decode($po,true);
        if (!empty($po['msg'])){
            return json_encode(['msg'=>$po['msg'],'code'=>'101']);
        }else{
            return json_encode(['msg'=>'领取成功','code'=>'0']);
        }
    }

    //查询当前用户的优惠券
    public function usercoupon($id){
        $Notify=  new Notify();
        $token=$Notify->kbxToken();
        if (empty($id)) return json_encode(['msg'=>'用户id出错','code'=>'101']);
        $user = db("member")->where('member_id',$id)->field("member_username")->find();
        $kbxToken = new KbxToken();
        //获取kbx的user_id
        $kbx = $kbxToken->kbxUser($user['member_username']);
        $url= $url=$this->api_url."/client/coupon/".$kbx['id']."/validCashCoupons";
        $get=$Notify->get_data($url,$token);
        return json_encode($get);
    }

    //锁定优惠券
    //暂时不用
    public function lockCoupons(){
        $url=$this->api_url."/client/coupon/lockCoupons";
        $Notify=  new Notify();
        $token=$Notify->kbxToken();
        $id= input('member_id');
        $normalCouponIds= input('normalCouponIds');
        if (empty($id)) return json_encode(['msg'=>'用户id出错','code'=>'101']);
        if (empty($normalCouponIds)) return json_encode(['msg'=>'旅行红包错误','code'=>'101']);
        $member=  db('member')->where('member_id',$id)->field('kbx_id')->find();
        $data=[
            'userId'=>$member['kbx_id'],
            'normalCouponIds'=>$normalCouponIds,
        ];
        $data=json_encode($data);
        dump($data);
        $get=$Notify->post_data($url,$data,$token);
        return $get;
    }

    //解除锁定优惠券
    //暂时不用
    public function unlockCoupons(){
        $url=$this->api_url."/client/coupon/unlockCoupons";
        $Notify=  new Notify();
        $token=$Notify->kbxToken();
        $id= input('member_id');
        $normalCouponIds= input('normalCouponIds');
        if (empty($id)) return json_encode(['msg'=>'用户id出错','code'=>'101']);
        if (empty($normalCouponIds)) return json_encode(['msg'=>'旅行红包错误','code'=>'101']);
        $member=  db('member')->where('member_id',$id)->field('kbx_id')->find();
        $data=[
            'userId'=>$member['kbx_id'],
            'normalCouponIds'=>$normalCouponIds,
        ];
        $data=json_encode($data);
        $get=$Notify->post_data($url,$data,$token);
       return $get;
    }

    //使用优惠券
    public function useCashCoupon($id,$cashCouponId,$orderPrice,$orderId){
        $url=$this->api_url."/client/coupon/useCashCoupon";
        $Notify=  new Notify();
        $token=$Notify->kbxToken();
        if (empty($id)) return json_encode(['msg'=>'用户id出错','code'=>'101']);
        if (empty($orderPrice)) return json_encode(['msg'=>'价格不能为空','code'=>'101']);
        if (empty($cashCouponId)) return json_encode(['msg'=>'旅行红包错误','code'=>'101']);
        if (empty($orderId)) return json_encode(['msg'=>'订单id错误','code'=>'101']);
        $member=  db('member')->where('member_id',$id)->field('kbx_id')->find();
        $data=[
            'userId'=>$member['kbx_id'],
            'cashCouponId'=>$cashCouponId,
            'orderPrice'=>$orderPrice,
            'orderId'=>$orderId,
        ];

        $data=json_encode($data);
        $get=$Notify->post_data($url,$data,$token);
        return $get;
    }


    //退回价格
    public function refundCashCoupon($id,$cashCouponId,$orderId){
        $url=$this->api_url."/client/coupon/refundCashCoupon";
        $Notify=  new Notify();
        $token=$Notify->kbxToken();
        if (empty($id)) return json_encode(['msg'=>'用户id出错','code'=>'101']);
        if (empty($cashCouponId)) return json_encode(['msg'=>'旅行红包错误','code'=>'101']);
        if (empty($orderId)) return json_encode(['msg'=>'订单id错误','code'=>'101']);
        $member=  db('member')->where('member_id',$id)->field('kbx_id')->find();
        $data=[
            'userId'=>$member['kbx_id'],
            'cashCouponId'=>$cashCouponId,
            'orderId'=>$orderId,
        ];
        $data=json_encode($data);
        $get=$Notify->post_data($url,$data,$token);
        return $get;
    }


    public function KBXremoveOrder(){
        $Notify= new Notify();
        $post=input('post.');
        $member= db('member')->where('member_id',$post['member_id'])->find();
        $url=$this->api_url.'/client/users/'.$member['kbx_id'].'/projects/'.$post['order_id'].'/cancelUnpaid';
        $token=   $Notify->kbxToken();
        $data=[
            'reason'=>'用户取消'
        ];
        $d= $Notify->post_data($url,json_encode($data),$token);
        $ds=json_decode($d,true);
        if (!empty($ds['msg']))return json_encode(['code'=>101,'msg'=>$ds['msg']]);

        return json_encode(['code'=>0,'msg'=>'取消成功']);
    }







}